<?php

class M_scholarships Extends MY_Model
{
	public function get_scholarships_available_for_stud($id)
	{
		$sql = "SELECT ss_id,ss_name,ss_rate 
				FROM student_scholarships
				WHERE ss_id NOT IN 
				(SELECT sdr_scholarship_id FROM student_deductions_record WHERE sdr_enrollment_id = ?)";
				
		$query = $this->db->query($sql,array($id));
		if($query->num_rows() >=1 )
		{
			$data[] = '';
			foreach($query->result() as $k => $v)
			{
				$data[$v->ss_id] = $v->ss_name.' | &#8369;'.money($v->ss_rate);
			}
			
			return $data;
		}else{
			return FALSE;
		}
	}
	
	public function get_selected_scholarship_and_insert_to_deductions($data)
	{
		$sql = "SELECT ss_id,ss_name,ss_desc,ss_rate
				FROM student_scholarships
				WHERE ss_id = ? ";
		$query = $this->db->query($sql,array($data['scholarships']));
		
		if($query->num_rows() >= 1)
		{
			$result = $query->row();
			
			$input['sdr_remarks'] = $data['remarks'];
			$input['sdr_amount'] = $result->ss_rate;
			$input['sdr_deduction_name'] = $result->ss_name;
			$input['sdr_enrollment_id'] = $data['enrollment_id'];
			$input['sdr_scholarship_id'] = $result->ss_id;
			$input['sdr_schoolyear_id'] = $this->c_sy->id;
			$input['sdr_user_trans_id'] = $this->c_user->userid;
			$input['sdr_created'] = NOW;
			
			$this->db->insert('student_deductions_record',$input);
			return $this->db->affected_rows() >=1 ? TRUE : FALSE;
		}else{
			return FALSE;
		}
	}
	
	public function get_student_deductions($id)
	{
		$sql = "SELECT sdr_amount,sdr_deduction_name
				FROM student_deductions_record
				WHERE sdr_enrollment_id = ?
				AND sdr_schoolyear_id = ?
				";
		$query = $this->db->query($sql,array($id,$this->c_sy->id));
		return $query->num_rows() >=1 ? $query->result() : FALSE;
	}
	
	public function sum_all_student_deductions($id)
	{
		$sql = "SELECT sum(sdr_amount) as total_deductions
				FROM student_deductions_record
				WHERE sdr_enrollment_id = ?
				AND sdr_schoolyear_id = ? ";
		$query = $this->db->query($sql,array($id,$this->c_sy->id));
		return $query->num_rows() >=1 ? $query->row()->total_deductions : FALSE;
	}
	
}